//
//  HotlesViewController.m
//  GoShops
//
//  Created by zcj on 15-12-8.
//  Copyright (c) 2015年 郑朝军. All rights reserved.
//

#import "HotlesViewController.h"
#import "GetUrlString.h"
#import "ShopTopNavBtn.h"
#import "MJRefresh.h"
#import "Grouble.h"
#import "SelectFindHttp.h"
#import "ImageScrollTableViewCell.h"
#import "ServiceAdModel.h"
#import "GetPlistArray.h"
#import "HotListTableViewCell.h"
#import "SlidesTableViewCell.h"
#import "SaleListModel.h"
#import "SortTableViewCell.h"
#import "HomeMoveMainTableViewCell.h"
#import "HomeMainModel.h"

#define kTujian @"http://e.dangdang.com/media/api.go?action=column&columnType=%@&start=%ld&end=%ld&channelType=all"

#define tWeini @"ts_children_girl"


@interface HotlesViewController ()<ShopTopNavBtnDelegate,MJRefreshBaseViewDelegate,UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong)NSMutableSet * saleListModel;
@property(nonatomic, strong)UITableView *firstTableView;
@property (nonatomic, strong)MJRefreshFooterView *foot;
@property (nonatomic, strong)MJRefreshHeaderView *head;
@property (nonatomic,strong)NSMutableArray * advArray;//广告数据源
@property (nonatomic,strong)NSArray * homeServiceArray;
@property (nonatomic,strong)NSMutableArray * advImageUrlArray;//广告图片数组

@property (nonatomic,strong)NSMutableArray * homeMoveArray;



@end

@implementation HotlesViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (NSMutableArray *)homeMoveArray
{
    if (_homeMoveArray == nil) {
        _homeMoveArray = [NSMutableArray array];
    }
    return _homeMoveArray;
}
- (NSMutableArray *)advArray
{
    if (_advArray == nil) {
        _advArray = [NSMutableArray array];
    }
    return _advArray;
}
- (NSArray *)homeServiceArray
{
    if (_homeServiceArray == nil) {
        _homeServiceArray = [GetPlistArray arrayWithString:@"menuData.plist"];
    }
    return _homeServiceArray;
}
- (NSMutableArray *)advImageUrlArray
{
    if (_advImageUrlArray == nil) {
        _advImageUrlArray = [NSMutableArray array];
    }
    return _advImageUrlArray;
}
- (NSMutableSet *)saleListModel
{
    if (_saleListModel == nil) {
        _saleListModel = [NSMutableSet setWithCapacity:20];
    }
    return _saleListModel;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self setNav];
    [self settable];
    [self setRush];
}
- (void)settable
{
    self.firstTableView = [UITableView initWithTableView:CGRectMake(0, 0, ScreenWidth, ScreenHeight) withDelegate:self];
    self.firstTableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.firstTableView];
}
- (void)setRush
{
    MJRefreshHeaderView * head = [[MJRefreshHeaderView alloc]init];
    head.scrollView = self.firstTableView;
    head.delegate = self;
    [head beginRefreshing];
    self.head = head;
    MJRefreshFooterView * foot = [[MJRefreshFooterView alloc]init];
    foot.scrollView = self.firstTableView;
    foot.delegate = self;
    self.foot = foot;
}
- (void)refreshViewBeginRefreshing:(MJRefreshBaseView *)refreshView
{
    if ([refreshView isKindOfClass:[MJRefreshFooterView class]]) { // 上拉刷新
        [self loadMoreData];
    } else { // 下拉刷新
        [self loadNewData];
    }
}
// 上啦加载
- (void)loadMoreData
{
    ZCJLog(@"jjloadMoreDatajjj");
    [self.foot endRefreshing];
}
// 下拉刷新
- (void)loadNewData
{
    ZCJLog(@"%@",self.homeServiceArray);
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        //  加载滚动条电影
        [self loadSlide];
        // 加载电影
        [self loadMoveData];
        // 加载滚动的电影
        [self loadSlideTop];
        
    });
}
- (void)dealloc
{
    [self.foot free];
    [self.head free];
}
// 加载滚动的电影
- (void)loadSlideTop
{
    NSString * weiniStr=[NSString stringWithFormat:kTujian,tWeini,(long)0,(long)2];
    [SelectFindHttp getWirhUrl:weiniStr parms:nil success:^(id json) {
        ZCJLog(@"%@",json);
        [self.saleListModel removeAllObjects];
        NSDictionary * dataDit = json[@"data"];
        NSArray * dataArray = dataDit[@"saleList"];
        
        for (NSDictionary * dict in dataArray) {
            SaleListModel * saleList = [SaleListModel objectWithKeyValues:dict];
            [self.saleListModel addObject:saleList];
        }
        for (NSDictionary * dict in dataArray) {
            SaleListModel * saleList = [SaleListModel objectWithKeyValues:dict];
            [self.saleListModel addObject:saleList];
        }
        
        ZCJLog(@"%d",self.saleListModel.count);
        [self.firstTableView reloadData];
        //ZCJLog(@"-----%d",self.saleListModel.count);
    } failture:^(id error) {
        
    }];
    if (self.advImageUrlArray.count == 0) {
        [self.advImageUrlArray addObject:@"http://img60.ddimg.cn/digital/product/19/1/1960071901_ii_cover.jpg?version=null"];
        [self.advImageUrlArray addObject:@"http://img60.ddimg.cn/digital/product/19/1/1960071901_ii_cover.jpg?version=null"];
    }
}
- (void)loadMoveData
{
    NSString * urlStr = nil;
    urlStr = [[GetUrlString sharedManager]urlWithHomeHomtel];
    
    [SelectFindHttp getWirhUrl:urlStr parms:nil success:^(id json) {
        NSArray * homeMoveArray = json[@"data"];
        for (NSDictionary * dict in homeMoveArray) {
            HomeMainModel * homeMainModel = [HomeMainModel objectWithKeyValues:dict];
            [self.homeMoveArray addObject:homeMainModel];
        }
        ZCJLog(@"%d",self.homeMoveArray.count);
        [self.firstTableView reloadData];
    } failture:^(id error) {
        ZCJLog(@"%@",error);
    }];
}
- (void)loadSlide
{
    NSString * urlAd  =[[GetUrlString sharedManager]urlWithVisitAd];
    [self.advImageUrlArray removeAllObjects];
    [self.advArray removeAllObjects];
    
    [SelectFindHttp getWirhUrl:urlAd parms:nil success:^(id json) {
       // ZCJLog(@"%@",json);
        
        NSMutableArray *dataArray = [json objectForKey:@"data"];
        for (int i = 0; i < dataArray.count;  ++i) {
            ServiceAdModel *serviceAdvM = [ServiceAdModel objectWithKeyValues:dataArray[i]];
            [self.advArray addObject:serviceAdvM];
            // 放两个是可以看到效果
            [self.advImageUrlArray addObject:serviceAdvM.imgUrl];
            [self.advImageUrlArray addObject:serviceAdvM.imgUrl];
            
        }
        
        [self.head endRefreshing];
        
    } failture:^(id error) {
        
    }];
}
- (void)setNav
{
    NSArray  *array = @[@"热卖",@"最新"];
    
    ShopTopNavBtn * shopTopNav = [[ShopTopNavBtn alloc]initWithFrame:CGRectMake(0, 0, 160, 40) titleArray:array];
    shopTopNav.delegate = self;
    self.navigationItem.titleView = shopTopNav;
}
#pragma mark - tableview代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4+self.homeMoveArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row == 0){
        ImageScrollTableViewCell * cell = [ImageScrollTableViewCell ImageScrollTableViewCell:tableView frame:CGRectMake(0, 0, ScreenWidth, 120)];
        if (self.advImageUrlArray.count !=0) {
            ZCJLog(@"%@",_advImageUrlArray);
            [cell setImageArray:self.advImageUrlArray];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }else if (indexPath.row ==  1){
        HotListTableViewCell * cell = [HotListTableViewCell HotListTableViewCell:tableView hotWithArray:self.homeServiceArray];
        return cell;
    }else if (indexPath.row == 2){
        SlidesTableViewCell * cell = [SlidesTableViewCell SlidesTableViewCell:tableView];
        if (self.saleListModel.count != 0) {
            for (int i = 0; i < self.saleListModel.count; i ++) {
                NSArray * saleArrayss = [self.saleListModel allObjects];
                
                SaleListModel * saleModel = saleArrayss[i];
                for (int i = 0 ; i < saleModel.mediaList.count; i ++) {
                    MediaListModel * mediaList = saleModel.mediaList[i];
                    cell.mediaModel = mediaList;
                }
            }
        }
        return cell;
    }else if (indexPath.row == 3){
        SortTableViewCell * cell = [SortTableViewCell SortTableViewCell:tableView];
        return cell;
    }else{
        HomeMoveMainTableViewCell * cell = [HomeMoveMainTableViewCell HomeMoveMainTableViewCell:tableView];
        cell.homeMainMove = self.homeMoveArray[indexPath.row-4];
        //        homeMoveMain
        return cell;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return 120;
    }else if (indexPath.row ==1){
        return 80;
    }else if (indexPath.row == 2){
        return 200;
    }else if (indexPath.row == 3){
        return 50;
    }
    return 150;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
